package com.rayboot.footpoint.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;

import com.rayboot.footpoint.db.DBManager.TableType;
import com.rayboot.footpoint.db.TableCategory.CategoryColumns;
import com.rayboot.footpoint.db.TableFootprints.FootprintColumns;
import com.rayboot.footpoint.model.Category;
import com.rayboot.footpoint.model.Footprint;

public class FootpointManager {
	private static FootpointManager mInstance = null;
	private static Context context;

	private FootpointManager(Context context) {
		this.context = context;
	}

	public static FootpointManager getInstance() {
		if (mInstance == null)
			mInstance = new FootpointManager(context);
		return mInstance;
	}

	public static void init(Context context) {
		if (mInstance == null) {
			mInstance = new FootpointManager(context);
			DBManager.init(context);
		}
	}

	public static void quit() {
		DBManager.quit();
	}

	private boolean checkContentValues(Footprint footprint) {
		if (TextUtils.isEmpty(footprint.mName)
				|| TextUtils.isEmpty(footprint.mPicPath)
				|| footprint.mCreateTime == 0L) {
			return false;
		}
		return true;
	}

	public boolean addFootprint(Footprint fp) {
		return checkContentValues(fp) ? DBManager.insert(
				TableType.FOOTPRINTS_TABLE, fp) : false;
	}

	public Cursor getAllCategory() {
		return DBManager.getAllData(TableType.CATEFORY_TABLE);
	}

	public ArrayList<Category> getAllCategoryArrayList() {
		Cursor cursor = getAllCategory();
		cursor.deactivate();
		ArrayList<Category> localArrayList = new ArrayList<Category>(
				cursor.getCount());
		while (cursor.moveToNext()) {
			Category category = new Category();
			fillCategoryDataFromCursor(category, cursor);
			localArrayList.add(category);
		}
		cursor.close();
		return localArrayList;
	}

	private void fillCategoryDataFromCursor(Category category, Cursor cursor) {
		category.mKeyword = cursor.getString(cursor
				.getColumnIndexOrThrow(CategoryColumns.KEYWORD));
		category.mName = cursor.getString(cursor
				.getColumnIndexOrThrow(CategoryColumns.NAME));
		category.mIconID = cursor.getInt(cursor
				.getColumnIndexOrThrow(CategoryColumns.ICON_ID));
	}

	public Cursor getCursorFootprintsByCategory(String category) {
		String rule = FootprintColumns.CATEGORY + "='" + category + "'";
		return DBManager.getDataBySomething(TableType.FOOTPRINTS_TABLE, rule);
	}

	public ArrayList<Footprint> getArrayFootprintsByCategory(String category) {
		Cursor cursor = getCursorFootprintsByCategory(category);
		ArrayList<Footprint> footprintArray = new ArrayList<Footprint>(
				cursor.getCount());
		// ArrayList footprintArray = new ArrayList(cursor.getCount());

		cursor.moveToFirst();
		while (cursor.moveToNext()) {
			Footprint fp = new Footprint(cursor);
			footprintArray.add(fp);
		}
		cursor.close();
		return footprintArray;
	}

	public Category getCategoryFromName(String categoryName) {

		String str = CategoryColumns.NAME + "='" + categoryName + "'";
		Cursor cursor = DBManager.getDataBySomething(TableType.CATEFORY_TABLE,
				str);
		Category category = null;
		if (cursor.moveToNext()) {
			category = new Category(cursor);
		}
		cursor.close();
		return category;
	}

}
